﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for KPIManualSerializer
    /// </summary>
    public class KPIManualSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                   "No",
                   "KPIDescription",
                   "isActive",
                   "Verificator",
                   "Weight",
                   "StrategicObjective",
                   "TargetDescription",
			       "ID"
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int UserID = HttpContext.Current.Request.Params["User"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.Params["User"]);
            int year = DateTime.Now.Year;// HttpContext.Current.Request.Params["Year"].Replace("undefined", string.Empty) == string.Empty ? DateTime.Now.Year : Convert.ToInt16(HttpContext.Current.Request.Params["Year"]);

            SetupYearFacade yf = new SetupYearFacade();
            SetupYear objYear = yf.GetByID(1);
            if (objYear != null)
                year = objYear.EndYear;

            IList<KPIAssignDetail> result = new List<KPIAssignDetail>();
            KPIAssignDetailFacade pf = new KPIAssignDetailFacade();
            int iTotalRecords = 0;
            result = pf.GetAllByUserID(sSearch, UserID, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords, year);
            int iTotalDisplayRecords = 0;

            iTotalDisplayRecords = iTotalRecords;

            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();

            if (result.Count > 0)
            {
                int idx = iDisplayStart;
                foreach (KPIAssignDetail p in result)
                {
                    idx++;
                    IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                    ColumnValues.Add(this.Columns[0], idx.ToString());
                    ColumnValues.Add(this.Columns[1], p.KPI == null ? string.Empty : p.KPI.Description.ToString());
                    ColumnValues.Add(this.Columns[2], p.isActive.ToString());
                    ColumnValues.Add(this.Columns[3], p.KPIAssign == null ? string.Empty : (p.KPIAssign.Verifier == null ? string.Empty : p.KPIAssign.Verifier.FirstName + " " + p.KPIAssign.Verifier.LastName.ToString()));
                    ColumnValues.Add(this.Columns[4], p.Weight.ToString());
                    ColumnValues.Add(this.Columns[5], p.KPI == null ? string.Empty : p.KPI.KPIStrategicObjective.ToString());
                    ColumnValues.Add(this.Columns[6], p.KPI == null ? string.Empty : p.KPI.TargetDescription.ToString());
                    ColumnValues.Add(this.Columns[7], p.ID.ToString());
                    collection.Add(idx, ColumnValues);
                }
            }

            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}